﻿using System;
using System.Windows.Forms;
using MATtutsLab.Entities;
using MATtutsLab.MVP.Presenters;
using MATtutsLab.MVP.Views;
using MATtutsLabs.ZombieMovieCatalog.Properties;

namespace MATtutsLabs.ZombieMovieCatalog
{
    public partial class AddEditZombieMovie : Form, IAddEditZombieMovieView 
    {
        private readonly ILogger Logger = new Logger();

        public AddEditZombieMovie()
        {
            InitializeComponent();
        }

        public AddEditZombieMoviePresenter Presenter { get; set; }

        public void DisableSave()
        {
            btnSave.Enabled = false;
        }

        public void SetTitleToAddMovie()
        {
            Text = Resources.AddEditZombieMovie_SetTitleToAddMovie;
        }

        public void EnableSave()
        {
            btnSave.Enabled = true;
        }

        public void HideTitleIsInvalidMessage()
        {
            epAddEditMovie.SetError(txtTitle, string.Empty);
        }

        public void ShowTitleIsInvalidMessage()
        {
            epAddEditMovie.SetError(txtTitle, Resources.AddEditZombieMovie_ShowTitleIsInvalidMessage);
            epAddEditMovie.SetIconAlignment(txtTitle, ErrorIconAlignment.MiddleRight);
        }

        public void HideShortDescriptionIsInvalidMessage()
        {
            epAddEditMovie.SetError(txtShortDescription, string.Empty);
        }

        public void ShowShortDescriptionIsInvalidMessage()
        {
            epAddEditMovie.SetError(txtShortDescription, Resources.AddEditZombieMovie_ShowShortDescriptionIsInvalidMessage);
            epAddEditMovie.SetIconAlignment(txtShortDescription, ErrorIconAlignment.MiddleRight);
        }

        public void ShowLongDescriptionIsInvalidMessage()
        {
            epAddEditMovie.SetError(txtLongDescription, Resources.AddEditZombieMovie_ShowLongDescriptionIsInvalidMessage);
            epAddEditMovie.SetIconAlignment(txtLongDescription, ErrorIconAlignment.MiddleRight);
        }

        public void HideLongDescriptionIsInvalidMessage()
        {
            epAddEditMovie.SetError(txtLongDescription, string.Empty);
        }

        public void ShowImageUrlIsInvalidMessage()
        {
            epAddEditMovie.SetError(txtImageUrl, Resources.AddEditZombieMovie_ShowImageUrlIsInvalidMessage);
            epAddEditMovie.SetIconAlignment(txtImageUrl, ErrorIconAlignment.MiddleRight);
        }

        public void HideImageUrlIsInvalidMessage()
        {
            epAddEditMovie.SetError(txtImageUrl, string.Empty);
        }

        public void LoadFormValuesFromExistingMovie(Movie existingMovie)
        {
            txtTitle.Text = existingMovie.Title;
            txtShortDescription.Text = existingMovie.ShortDescription;
            txtLongDescription.Text = existingMovie.LongDescription;
            txtImageUrl.Text = existingMovie.Image;
        }

        public void CloseRequested()
        {
            Close();
        }

        public void SetTitleToEditMovie()
        {
            Text = Resources.AddEditZombieMovie_SetTitleToEditMovie;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Presenter.SaveRequested();
            }
            catch (Exception ex)
            {
                Logger.WriteToLog(ex.Message);
            }
        }

        private void txtTitle_TextChanged(object sender, EventArgs e)
        {
            try
            {
                Presenter.TitleEntered(txtTitle.Text);
            }
            catch (Exception ex)
            {
                Logger.WriteToLog(ex.Message);
            }
        }

        private void txtShortDescription_TextChanged(object sender, EventArgs e)
        {
            try
            {
                Presenter.ShortDescriptionEntered(txtShortDescription.Text);
            }
            catch (Exception ex)
            {
                Logger.WriteToLog(ex.Message);
            }
        }

        private void txtLongDescription_TextChanged(object sender, EventArgs e)
        {
            try
            {
                Presenter.LongDescriptionEntered(txtLongDescription.Text);
            }
            catch (Exception ex)
            {
                Logger.WriteToLog(ex.Message);
            }
        }

        private void txtImageUrl_TextChanged(object sender, EventArgs e)
        {
            try
            {
                Presenter.ImageUrlEntered(txtImageUrl.Text);
            }
            catch (Exception ex)
            {
                Logger.WriteToLog(ex.Message);
            }
        }
    }
}
